﻿namespace ApiTemplate.WebApi
{
    /// <summary>
    /// 注入sqlsugar
    /// </summary>
    public static class SqlsugarSetup
    {
        /// <summary>
        /// 注入sqlsugar
        /// </summary>
        /// <param name="services"></param>
        /// <param name="appSetting"></param>
        public static void AddSqlsugarSetup(this IServiceCollection services, AppSetting appSetting)
        {
            services.AddScoped<TransactionFilter>();
            services.AddHttpContextAccessor();
            DbType dbType = (DbType)appSetting.DataBaseType;

            ConnectionConfig connectionConfig = new()
            {
                ConnectionString = appSetting.ConnStr,
                DbType = dbType,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            };
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                SqlSugarScope sqlSugar = new(connectionConfig);
                return sqlSugar;
            });
        }
    }
}
